<script setup>
import { ref, computed, defineProps } from 'vue'
import { useRouter } from 'vue-router'
import { updateOrderStatus, cancelRefund  } from '@/api/order'
import { showToast } from 'vant';
const router = useRouter()
  const myOrderList = defineProps({
    myOrderList: {
      type: Array,
    }
})
const emit = defineEmits(['updateList'])
const pay = (orderId) => {
  router.push({
    path: '/pay',
    query: {
      orderId: orderId
    }
  })
}
const updateStatus = async (status,orderId) => {
  await updateOrderStatus(status,orderId)
  if (status === '6') {
    showToast('申请退款中')
  } else if (status === '3') {
    showToast('确认收货成功')
  }
  emit('updateList')
}
// 取消退款
const cancel = async (orderId) => {
  await cancelRefund(orderId)
  showToast('取消退款成功')
  emit('updateList')
}
</script>


<template>
  <div class="order-list-item" v-for="item in myOrderList.myOrderList" :key="item.id">
    <div class="tit">
      <div class="time">{{ item.createTime }}</div>
      <div class="status">
        <span v-if="item.status == '5'">待支付</span>
        <span v-if="item.status == '1'">待发货</span>
        <span v-if="item.status == '2'">待收货</span>
        <span v-if="item.status == '3'">已完成</span>
        <span v-if="item.status == '4'">已退款</span>
        <span v-if="item.status == '6'">退款申请中</span>
      </div>
    </div>
    <div class="list" >
      <div class="list-item" v-for="orderList in item.orderList" :key="orderList.id">
        <div class="goods-img">
          <img :src="orderList.image" alt="">
        </div>
        <div class="goods-content text-ellipsis-2">
          {{ orderList.name }}
        </div>
        <div class="goods-trade">
          <p>¥ {{ orderList.amount * orderList.number}}</p>
          <p>x 3</p>
        </div>
      </div>
    </div>
    <div class="total">
      共{{ item.allCount }}件商品，总金额 ¥{{ item.allPrice }}
    </div>
    <div class="actions">
      <span v-if="item.status == '5'" @click="pay(item.id)">立刻付款</span>
      <span v-if="item.status == '6'" @click="cancel(item.id)">取消退款</span>
      <span v-if="item.status == '2'" @click="updateStatus(3,item.id)">确认收货</span>
      <span v-if="item.status == '5' || item.status == '3' ||item.status == '1' || item.status == '2'" @click="updateStatus(6,item.id)">申请退款</span>
    </div>
  </div>
</template>


<style lang="less" scoped>
.order-list-item {
  margin: 10px auto;
  width: 94%;
  padding: 15px;
  background-color: #ffffff;
  box-shadow: 0 0.5px 2px 0 rgba(0,0,0,.05);
  border-radius: 8px;
  color: #333;
  font-size: 13px;

  .tit {
    height: 24px;
    line-height: 24px;
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
    .status {
      color: #fa2209;
    }
  }

  .list-item {
    display: flex;
    .goods-img {
      width: 90px;
      height: 90px;
      margin: 0px 10px 10px 0;
      img {
        width: 100%;
        height: 100%;
      }
    }
    .goods-content {
      flex: 2;
      line-height: 18px;
      max-height: 36px;
      margin-top: 8px;
    }
    .goods-trade {
      flex: 1;
      line-height: 18px;
      text-align: right;
      color: #b39999;
      margin-top: 8px;
    }
  }

  .total {
    text-align: right;
  }
  .actions {
    text-align: right;
    span {
      display: inline-block;
      height: 28px;
      line-height: 28px;
      color: #383838;
      border: 0.5px solid #a8a8a8;
      font-size: 14px;
      padding: 0 15px;
      border-radius: 5px;
      margin: 10px 0;
      margin-left: 10px;
    }
  }
}
</style>